package com.myapp.actions.visit;

import com.myapp.entity.Client;
import com.myapp.entity.Visit;
import com.myweb.WebAction;
import com.myweb.WebException;
import com.myweb.WebRequest;
import com.myweb.WebResponse;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

public class AddVisit implements WebAction {

  private Visit visit = new Visit();

  @Override
  public void prepare(WebRequest request) throws WebException {
    Client client = new Client();
    client.setId(request.getLongNN("clientId"));
    visit.setClient(client);
    visit.setDeleted(false);
    visit.setVisitTime(new Date());
    visit.setReasonForConsult(request.getStringNN("reasonForConsult"));
    visit.setHistoryOfPresentIllness(request.getString("historyOfPresentIllness"));
    visit.setSubjective(request.getString("subjective"));
    visit.setObjective(request.getString("objective"));
    visit.setAssessment(request.getString("assessment"));
    visit.setPlan(request.getString("plan"));
  }

  @Override
    public EntityManager process(WebRequest req, WebResponse res, EntityManager em) throws Exception {
    em.persist(visit);
    res.setMessage("Successfully added visit");
    res.setResult(visit);
    return em;
  }
}
